// Lee dialog 1.0 http://www.xij.cn/blog/?p=68
var doFun=-1;//最终执行操作
var closeFun=-1;//关闭时执行操作
var dialogFirst = true;
var confirmFlag = false;
var tempHtml="-1";//临时空html
var divid="-1";

/**
 * 
 * @param title
 * @param content
 * 		  usage: e.g. url:post?sg-home/center/request.html?var1:3,var2:4
 * @param width
 * @param height
 * @param cssName
 * @return
 */
function dialog(title, content, width, height, cssName){
	
    if (dialogFirst == true) {
        var temp_float = new String;
        temp_float = "<div id=\"floatBoxBg\" style=\"height:" + $(document).height() + "px;filter:alpha(opacity=0);opacity:0;\"></div>";
        temp_float += "<div id=\"floatBox\" class=\"floatBox\">";
        temp_float += "<div class=\"floatBox_title\"><h4></h4><span>关闭</span></div>";
        temp_float += "<div class=\"floatBox_content\"></div>";
        temp_float += "</div>";
        $("body").append(temp_float);
        dialogFirst = false;
        $("#floatBox .floatBox_title span").click(function(){
    		closeDialog();
//            $("#floatBoxBg").animate({
//                opacity: "0"
//            }, "normal", function(){
//                $(this).hide();
//            });
//            $("#floatBox").animate({
//                top: ($(document).scrollTop() - (height == "auto" ? 300 : parseInt(height))) + "px"
//            }, "normal", function(){
//                $(this).hide();
//            });
        });
        
    }
    
    $("#floatBox .floatBox_title h4").html(title);
    contentType = content.substring(0, content.indexOf(":"));
    content = content.substring(content.indexOf(":") + 1, content.length);
    switch (contentType) {
        case "url":
            var content_array = content.split("?");
//            $("#floatBox .floatBox_content").ajaxStart(function(){
//                $(this).html("loading...");
//            });
            var jsonData = eval("({" + content_array[2] + "})");
            $.ajax({
                type: content_array[0],
                url: content_array[1],
                data: jsonData,
                error: function(){
                    $("#floatBox .floatBox_content").html("error...");
                },
                success: function(html){
                    $("#floatBox .floatBox_content").html(html);
                }
            });
            break;
        case "text":
            $("#floatBox .floatBox_content").html(content);
            break;
        case "id":
			//fix for formValidator start
			this.divid=content;
			this.tempHtml=$("#" + content + "").html();
			//fix for formValidator end
            $("#floatBox .floatBox_content").html(tempHtml);
			$("#" + content + "").html("");
            break;
        case "iframe":
            $("#floatBox .floatBox_content").html("<iframe src=\"" + content + "\" width=\"100%\" height=\"" + (parseInt(height) - 30) + "px" 
										+ "\" scrolling=\"auto\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\"></iframe>");
    }
    
    $("#floatBoxBg").show();
    $("#floatBoxBg").animate({
        opacity: "0.5"
    }, "normal");
    $("#floatBox").attr("class", "floatBox " + cssName);
    $("#floatBox").css({
        display: "block",
        left: (($(document).width()) / 2 - (parseInt(width) / 2)) + "px",
        top: ($(document).scrollTop() - (height == "auto" ? 300 : parseInt(height))) + "px",
        width: width,
        height: height
    });
    $("#floatBox").animate({
        top: ($(document).scrollTop() + 50) + "px"
    }, "normal");
}

function openConfirmDiolog(confirm_title, confirm_message, recallFun){
    this.doFun = recallFun;
    document.getElementById("confirm_title").innerHTML = confirm_title;
    document.getElementById("confirm_message").innerHTML = confirm_message;
    dialog("确认操作", "id:confirm_div", "304px", "auto", "id");
    return this.confirmFlag;
}

function closeDialog(){
	if(tempHtml!="-1"){
		$("#" + divid + "").html(tempHtml);
		tempHtml="-1";
		divid="-1";
	};
    $("#floatBoxBg").hide();
    $("#floatBox").hide();
	
    this.confirmFlag = false;
    if(this.closeFun!=null&&this.closeFun!=-1){
    	closeFun();
		closeFun=-1;
    }
    return this.confirmFlag;
}

function confirmDiolog(){
	
    $("#floatBoxBg").hide();
    $("#floatBox").hide();
    this.confirmFlag = true;
    return this.confirmFlag;
}

//通用 弹出操作返回结果提示框 title:提示框title result_title 结果title result_message:结果详细信息 flag:success=成功信息 failed=失败信息
function showResult(title, result_title, result_message, flag){
    document.getElementById(flag + "-message-title").innerHTML = result_title;
    document.getElementById(flag + "-message").innerHTML = result_message;
    dialog(title, "id:" + flag + "-pop-div", "304px", "auto", "id");
}

//通用 弹出操作返回结果提示框 title:提示框title result_title 结果title result_message:结果详细信息 flag:success=成功信息 failed=失败信息  recall 回调函数
function showResultCallBack(title, result_title, result_message, flag,recall){
    document.getElementById(flag + "-message-title").innerHTML = result_title;
    document.getElementById(flag + "-message").innerHTML = result_message;
    dialog(title, "id:" + flag + "-pop-div", "304px", "auto", "id");
    closeFun=recall;
}